#include "stdafx.h"
#include "HbCurlExport.h"
#include "UI/QHBShowDialog.h"
#include "HBCurl/HBCurl.h"

namespace HBCurl
{
	void CHBLoadItemListener::PushDetail(const DetailInfo& detailInfo)
	{
		OnDetail(detailInfo);

		if (LMT_ERROR == detailInfo.m_msgType)
		{
//			OnErrorOccur(detailInfo.m_sUrl, detailInfo.m_sLocalPath, detailInfo.m_iDetailCode, detailInfo.m_sDetailMsg);
		}
		else if (LMT_FINISH == detailInfo.m_msgType)
		{
// 			OnFinish(detailInfo.m_sUrl, detailInfo.m_sLocalPath);
		}
	}

	int Init( CHBLoadItemListener* pMsgListener /*= NULL*/ )
	{
		return CHBEasyCurl::Init(pMsgListener);
	}

	string GetErrorMsg( int iErrorCode )
	{
		return "";
	}

	int DownloadFile( const string& sUrl, const string& sLocalPath, 
		const string& sUserPwd, bool bSync /*= false*/ )
	{
		return CHBEasyCurl::DownloadFile(QString::fromStdString(sUrl), QString::fromStdString(sLocalPath), 
			QString::fromStdString(sUserPwd), bSync);
	}

	int DownloadFile( const string& sUrl, const string& sLocalPath, const string& sUserPwd, 
		long long iStartPos, long long iEndPos, bool bCreateWholeFile /*= true*/, bool bSync /*= false*/ )
	{
		qint64 iFileSize = CHBEasyCurl::GetRemoteFileSize(QString::fromStdString(sUrl), QString::fromStdString(sUserPwd));
		if (iFileSize >= 0)
		{
			return CHBEasyCurl::DownloadFile(QString::fromStdString(sUrl), QString::fromStdString(sLocalPath), 
				QString::fromStdString(sUserPwd), iStartPos, iEndPos, iFileSize, bCreateWholeFile, bSync);
		}

		return CHBEasyCurl::GetLastInfo().m_iDetailCode;
	}

	int UploadFile( const string& sUrl, const string& sLocalPath, 
		const string& sUserPwd, bool bSync /*= false*/ )
	{
		return CHBEasyCurl::UploadFile(QString::fromStdString(sUrl), QString::fromStdString(sLocalPath), 
			QString::fromStdString(sUserPwd), bSync);
	}

	int GetRemoteFileSize( const string& sUrl, const string& sUserPwd )
	{
		return CHBEasyCurl::GetRemoteFileSize(QString::fromStdString(sUrl), QString::fromStdString(sUserPwd));
	}

	void ShowHBCurlDialog()
	{
		QApplication& a = *(CHBEasyCurl::GetQApp());
		int argc=0;
		QDialog * pDlg = new QHBShowDialog();
		pDlg->show();
		a.exec();
	}
}
